﻿package Cain.Bit
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.system.System;
	/**
	 * 粒子發射器 v1.01 2010.9.23
	 * @author Cain@2009 in ADCAST
	 */
	public class Bit_Transmitter 
	{
		private var fn_mv:Sprite;
		private var bit:String;
		private var num:int;
		private var maxax:Number;
		private var maxay:Number;
		private var minscale:Number;
		private var time:Number;
		private var live:int;
		private var _sc:Number;
		private var _time_st:Number;
		private var vx:Number;
		private var vy:Number;
		private var g:Number;
		private var wind:Number;
		/**
		 * 發射器 參數
		 * _maxscale 縮放最大
		 * _minscale 縮放最小
		 * _maxax 最高x加速度
		 * _maxay 最高y加速度
		 * _vx x初速
		 * _vy y初速
		 * _g 重力
		 * _wind 風向
		 * _live 粒子壽命
		 */
		public var acc:Object;
		/**
		 * 發射器設定
		 * @param	_fn_mv 發射容器
		 */
		public function Bit_Transmitter(_fn_mv:Sprite) 
		{
			fn_mv = _fn_mv;
			init();
		}
		
		private function init():void
		{
			acc = {
				_maxscale:1,
				_minscale:.5,
				_maxax:.5,
				_maxay:.5,
				_vx:0,
				_vy:0,
				_g:0,
				_wind:0,
				_live:500
			}
		}
		/**
		 * 炸射一次
		 * @param	_bit 元件庫中套用的粒子的顯示物件 Class 名
		 * @param	_num 炸射數量
		 */
		public function bom(_bit:String,_num:int=30):void 
		{
			bit = _bit;
			num =  _num;
			maxax = acc._maxax;
			maxay = acc._maxay;
			live = acc._live;
			minscale = acc._minscale;
			_sc = acc._maxscale-acc._minscale;
			vx = acc._vx;
			vy = acc._vy;
			g = acc._g;
			wind = acc._wind;
			for (var i:int = 0; i < num; i++) 
			{
				var bb:Bit = new Bit(bit, maxax, maxay, live);
				bb.scaleX = bb.scaleY = minscale + Math.random() * _sc;
				bb._vx = vx;
				bb._vy = vy;
				bb._g = g;
				bb._wind = wind;
				fn_mv.addChild(bb);
			}
		}
		/**
		 * 噴射
		 * @param	_bit 元件庫中套用的粒子的顯示物件 Class 名
		 * @param	_time 發射時間序
		 */
		public function play(_bit:String,_time:Number=1):void 
		{
			_time_st = 0;
			bit = _bit;
			maxax = acc._maxax;
			maxay = acc._maxay;
			time = _time;
			live = acc._live;
			minscale = acc._minscale;
			_sc = acc._maxscale-acc._minscale;
			vx = acc._vx;
			vy = acc._vy;
			g = acc._g;
			wind = acc._wind;
			if (time>=1) 
			{
				fn_mv.addEventListener(Event.ENTER_FRAME, runing);
			}else {
				fn_mv.addEventListener(Event.ENTER_FRAME, runing2);
			}
			fn_mv.addEventListener(Event.REMOVED_FROM_STAGE, onREMOVED_FROM_STAGE);
		}
		
		private function onREMOVED_FROM_STAGE(e:Event):void 
		{
			fn_mv.removeEventListener(Event.REMOVED_FROM_STAGE, onREMOVED_FROM_STAGE);
			stop();
			fn_mv = null;
			bit = null;
		}
		/**
		 * 停止發射
		 */
		public function stop():void 
		{
			fn_mv.removeEventListener(Event.ENTER_FRAME, runing);
			fn_mv.removeEventListener(Event.ENTER_FRAME, runing2);
		}
		private function runing(e:Event):void 
		{
			for (var i:int = 0; i < time; i++) 
			{
				var bb:Bit = new Bit(bit, maxax, maxay, live);
				bb.scaleX = bb.scaleY = minscale + Math.random() * _sc;
				bb._vx = vx;
				bb._vy = vy;
				bb._g = g;
				bb._wind = wind;
				fn_mv.addChild(bb);
			}
		}
		private function runing2(e:Event):void 
		{
			_time_st += time;
			if (_time_st>=1) 
			{
				var bb:Bit = new Bit(bit, maxax, maxay, live);
				bb.scaleX = bb.scaleY = minscale + Math.random() * _sc;
				bb._vx = vx;
				bb._vy = vy;
				bb._g = g;
				bb._wind = wind;
				fn_mv.addChild(bb);
				_time_st--;
			}		
		}
	}
	
}